package city.spring.utils;

import java.lang.reflect.Field;

/**
 * Bean 对象操作工具类
 *
 * @author HouKunLin
 * @date 2020/7/17 0017 11:02
 */
public class BeanUtils {
    public static <P, C extends P> C toChildren(P parent, Class<C> childrenClass) {
        C instance = null;
        try {
            instance = childrenClass.newInstance();
            Class<?> parentClass = parent.getClass();
            Field[] parentFields = parentClass.getDeclaredFields();
            for (Field parentField : parentFields) {
                if (!parentField.isAccessible()) {
                    parentField.setAccessible(true);
                }
                parentField.set(instance, parentField.get(parent));
            }
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
        }
        return instance;
    }
}
